package com.linked.order.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.linked.order.feign.IPayFeign;
import com.linked.order.order.bean.po.OrderInfoPO;
import com.linked.order.order.mapper.IOrderInfoMapper;
import com.linked.order.order.service.IOrderHandleService;
import org.springframework.stereotype.Service;

/**
 * @author :dbq
 * @date : 2023/5/26 15:53
 * @description : desc
 */
@Service
public class OrderHandleServiceImpl implements IOrderHandleService {

    private final IOrderInfoMapper orderInfoMapper;

    private final IPayFeign payFeign;

    public OrderHandleServiceImpl(IPayFeign payFeign, IOrderInfoMapper orderInfoMapper) {
        this.orderInfoMapper = orderInfoMapper;
        this.payFeign = payFeign;
    }

    @Override
    public boolean createOrder(OrderInfoPO orderInfo) {
        return orderInfoMapper.insert(orderInfo) > 0;
    }

    @Override
    public boolean updateOrderStatus(String payId, Integer orderStatus) {
        UpdateWrapper<OrderInfoPO> wrapper = new UpdateWrapper<>();
        wrapper.eq("pay_id", payId);
        OrderInfoPO tmp = new OrderInfoPO();
        tmp.setOrderStatus(orderStatus);
        return orderInfoMapper.update(tmp, wrapper) > 0;
    }

    @Override
    public boolean sendReceivedMessage(String payId) {

        try {
            payFeign.receiveSendPaymentResultBack(payId);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
